-- 补发处理中心

local mis_timer = require('modules.commhm.ngxTimerWrap')

local ngx = ngx

local mis_log = nil
local mis_config = nil
local mis_mysql = nil
local mis_common = nil
local mis_bridge = nil
local mis_calcu = nil
local t_retCode = nil

--------------------------------------------------------------
local mis_saveAss = {
	s_name = string.match(debug.getinfo(1).short_src, '.*/(.*).lua');	-- 模块名称
}
local this = mis_saveAss
local t_m_mysqlConf = nil

--------------------------------------------------------------
-- 说明：补发循环
local function save_ass_loop()
	local n_code
	local s_keys
	local ts_mysql, ts_res
	local t_mailContent = mis_config.t_mail.t_content

	repeat
		-- 检索
		n_code, ts_mysql = mis_mysql.new(t_m_mysqlConf)
		if n_code ~= t_retCode.n_OK then
			break
		end
		n_code, ts_res = ts_mysql:query('SELECT `key`, `uin`, `data`, `from`, `time` FROM `' ..
								t_m_mysqlConf.s_table .. '` LIMIT ' .. mis_config.n_saveAssNum)
		if n_code ~= t_retCode.n_OK then
			ts_mysql:keepalive()
			break
		end
		-- 发邮件
		for _, t_ in pairs(ts_res) do
			n_code = mis_bridge.send_email(t_.uin, (t_mailContent[t_.from] or t_mailContent.default).s_title,
							(t_mailContent[t_.from] or t_mailContent.default).s_body, 32503651200, mis_common.json_to_table(t_.data))
			if n_code ~= t_retCode.n_OK then
				mis_log.day('saveAss', 'stop! send email failed')
				return
			end
			s_keys = s_keys and (s_keys .. ',' .. t_.key) or (t_.key)
			mis_log.day('saveAss', t_.uin .. '|' .. t_.from .. '|' .. t_.data)
		end
		if not s_keys then
			break
		end
		-- 删除
		n_code, ts_res = ts_mysql:query('DELETE FROM `' .. t_m_mysqlConf.s_table .. '` WHERE `key` IN (' .. s_keys .. ')')
		ts_mysql:keepalive()
		if n_code ~= t_retCode.n_OK then
			mis_log.day('saveAss', 'stop! ' .. ts_res)
			return
		end
	until true

	mis_timer:new():ngx_timer_at(mis_config.n_saveAssInterval, save_ass_loop)
end

--------------------------------------------------------------
-- 说明：模块包含
-- 参数：模块表
this.modules_include = function(t_moddules)
	mis_log = t_moddules.mis_log
	mis_config = t_moddules.mis_config
	mis_mysql = t_moddules.mis_mysql
	mis_common = t_moddules.mis_common
	mis_bridge = t_moddules.mis_bridge
	mis_calcu = t_moddules.mis_calcu
	t_retCode = mis_config.t_retCode
end

--------------------------------------------------------------
-- 说明：load级初始化
this.init_load = function()
	t_m_mysqlConf = mis_config.t_mysql.t_saveAss
	if t_m_mysqlConf and mis_calcu.is_master_server() == true then
		-- 建表
		local n_code, ts_res = mis_mysql.query_once(t_m_mysqlConf,
						'CREATE TABLE IF NOT EXISTS `' .. t_m_mysqlConf.s_table .. '` \
						( \
						`key`		INT 			NOT NULL AUTO_INCREMENT, \
						`uin`		INT(20)			NOT NULL, \
						`data`		VARCHAR(1000)	NOT NULL, \
						`from`		VARCHAR(50)		NOT NULL, \
						`time`		INT 			NOT NULL, \
						PRIMARY KEY (`key`) \
						) ENGINE=MyISAM CHARSET=utf8;')
		if n_code ~= t_retCode.n_OK then
			return n_code, ts_res
		end
		-- 开启循环
		if ngx.worker.id() == 0 then
			local _, s_err = mis_timer:new():ngx_timer_at(mis_config.n_saveAssInterval, save_ass_loop)
			if s_err then
				return t_retCode.n_SYS_ERR, 'start ngx.timer.every failed, ' .. s_err
			else
				mis_log.day('saveAss', 'start!')
			end
		end
	end
	return t_retCode.n_OK
end

--------------------------------------------------------------
-- 说明：生成补发任务
-- 参数：n_uin			迷你号
--		n_from			业务来源
-- 		s_data			补发数据,格式为table_to_json字符串
--						{{id=0, num=0},{...}}
-- 返回：n				状态码
--		s 				描述
this.add_job = function(n_uin, n_from, s_data)
	if not t_m_mysqlConf then
		return t_retCode.n_OK
	end
	if n_uin and n_uin < 1000 then
		mis_log.day('saveAss', 'saveAss add job failed!' .. '|' .. n_uin .. '|' .. n_from .. '|' .. s_data)
		return t_retCode.n_OK
	end
	return mis_mysql.query_once(
							t_m_mysqlConf,
							'INSERT INTO `' .. t_m_mysqlConf.s_table .. '`(`uin`, `data`, `from`, `time`) VALUES(' ..
							n_uin .. ', \'' .. s_data .. '\', \'' .. n_from .. '\', ' .. ngx.time() .. ')')
end

return this